package com.neuedu.his.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neuedu.his.po.Registlevel;
import com.neuedu.his.service.RegistlevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/master/reglevel")
public class RegistLevelController {
    @Autowired
    private RegistlevelService regLevelService;

    /**
     * 列表（支持关键字模糊搜索）
     */
    @GetMapping
    public List<Registlevel> list(@RequestParam(required = false) String keyword) {
        LambdaQueryWrapper<Registlevel> qw = new LambdaQueryWrapper<>();
        qw.eq(Registlevel::getDelmark, 1);
        if (keyword != null && !keyword.isEmpty()) {
            qw.and(w -> w
                    .like(Registlevel::getRegistcode, keyword)
                    .or().like(Registlevel::getRegistname, keyword)
            );
        }
        qw.orderByAsc(Registlevel::getSequenceno);
        return regLevelService.list(qw);
    }

    /**
     * 新增
     */
    @PostMapping
    public boolean save(@RequestBody Registlevel reg) {
        reg.setDelmark(1);
        return regLevelService.save(reg);
    }

    /**
     * 编辑
     */
    @PutMapping("/{id}")
    public boolean update(
            @PathVariable Integer id,
            @RequestBody Registlevel reg) {
        reg.setId(id);
        return regLevelService.updateById(reg);
    }

    /**
     * 获取单条
     */
    @GetMapping("/{id}")
    public Registlevel getById(@PathVariable Integer id) {
        return regLevelService.getById(id);
    }

    /**
     * 删除（逻辑删除）
     */
    @DeleteMapping("/{id}")
    public boolean deleteById(@PathVariable Integer id) {
        Registlevel tmp = new Registlevel();
        tmp.setId(id);
        tmp.setDelmark(0);
        return regLevelService.updateById(tmp);
    }
}
